var express = require("express");
var app = express();

var bodyParser = require('body-parser');
var multer = require('multer');
// session 模块
var session = require('express-session');





// 设定views变量，意为视图存放的目录
app.set('views', __dirname + '/views/');
// 修改模板文件的后缀名为html
app.set( 'view engine', 'html' );
// 运行ejs模块
app.engine( '.html', require( 'ejs' ).__express );

// 有了静态目录文件，我们还得在启动文件里告诉它这个静态文件路径，需要指定一下，如下所示
app.use(express.static(require('path').join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());

app.use(session({
    secret:'secret',
    resave:true,
    saveUninitialized:false,
    cookie:{
        maxAge:1000*60*10  //过期时间设置(单位毫秒)
    }
}));

app.use(function(req, res, next){
    res.locals.user = req.session.user;
    var err = req.session.error;
    res.locals.message = '';
    if (err) res.locals.message = '<div style="margin-bottom: 20px;color:red;">' + err + '</div>';
    next();
});

require('./public/javascripts/route/login')(app);
require('./public/javascripts/route/home')(app);
require('./public/javascripts/route/logout')(app);

// app.get('/logout', function(req, res){
//     req.session.user = null;
//     req.session.error = null;
//     res.redirect('index');
// });
//
app.get('/index', function(req, res) {
    res.render('index');
});
//
app.get('/', function(req, res) {
    res.render('index');
});
//
// app.get('/login', function (req, res) {
//    res.render('login');
// });
// app.get('/home', function (req, res) {
//     if(req.session.user){
//         res.render('home');
//     }else{
//         req.session.error = "请先登录"
//         res.redirect('login');
//     }
// });
// app.post('/login', function (req, res) {
//     var user={
//         username:'admin',
//         password:'admin'
//     };
//     if(req.body.username==user.username&&req.body.password==user.password)
//     {
//         req.session.user = user;
//         res.send(200);
//     }else{
//         res.send( 404 );
//     }
// });



app.listen(80);